package tms.view;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import tms.controler.AccountController;
import tms.model.Account;
import java.awt.Component;
import javax.swing.JLabel;

public class RegisterView extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel parent;
    private ContactInfoPanel panelContactInfo;
    private ExtraInfoPanel panelExtraRegisterInfo;
    private JPanel panelFooter;

    final AccountController accountController;

    public RegisterView(final Account acc, boolean isEditAccount) {
        setResizable(false);
        if (isEditAccount) {
            setTitle("Edit your account");
        } else {
            setTitle("Register new account");
        }

        accountController = new AccountController();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 743, 316);
        parent = new JPanel();
        parent.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(parent);

        panelContactInfo = new ContactInfoPanel();
        panelContactInfo.setBounds(10, 36, 707, 135);
        panelExtraRegisterInfo = new ExtraInfoPanel();
        panelExtraRegisterInfo.setBounds(10, 171, 707, 72);
        parent.setLayout(null);

        JLabel lblCaption;
        if (isEditAccount) {
            lblCaption = Tools.createLabel("Change your account information below or " +
                    "close this if you don't want to change anything.", Tools.TAHOMA_BOLD_12);
        } else {
            lblCaption = Tools.createLabel("To register a new account, you must provides " +
                    "your information here:", Tools.TAHOMA_BOLD_12);
        }
        lblCaption.setBounds(10, 11, 743, 14);
        parent.add(lblCaption);

        parent.add(panelContactInfo);
        parent.add(panelExtraRegisterInfo);

        panelFooter = new JPanel();
        panelFooter.setBounds(10, 250, 707, 25);
        panelFooter.setAlignmentX(Component.RIGHT_ALIGNMENT);
        panelFooter.setPreferredSize(new Dimension(743, 25));
        parent.add(panelFooter);
        panelFooter.setLayout(null);

        String buttonName = "Register";

        if (acc != null) {
            buttonName = "Save";
            panelContactInfo.setTitle(acc.getTitle());
            panelContactInfo.setFname(acc.getFirstName());
            panelContactInfo.setLname(acc.getLastName());
            panelContactInfo.setDOB(acc.getBirthDate());
            panelContactInfo.setAddress(acc.getAddress());
            panelContactInfo.setPhoneNumber(acc.getPhone());
            panelContactInfo.setEmail(acc.getEmail());
            panelContactInfo.setCountry(acc.getCountry());
            panelExtraRegisterInfo.setPassportNumber(acc.getPassportNumber());
            panelExtraRegisterInfo.setIssuingCountry(acc.getIssuingCountry());
        }

        Tools.addJButton(panelFooter, buttonName, null, 0, 0, 89, 23, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                if (panelContactInfo.checkValidInput() && panelExtraRegisterInfo.checkValidPassword()) {

                    String pw = panelExtraRegisterInfo.getPassword();
                    String title = panelContactInfo.getTitle();
                    String fname = panelContactInfo.getFname();
                    String lname = panelContactInfo.getLname();
                    String email = panelContactInfo.getEmail();
                    String phone = panelContactInfo.getPhoneNumber();
                    String address = panelContactInfo.getAddress();
                    String country = panelContactInfo.getCountry();
                    String passportNumber = panelExtraRegisterInfo.getPassportNumber();
                    String issuingCountry = panelExtraRegisterInfo.getIssuingCountry();

                    boolean res = accountController.createOrUpdate(acc, title, fname, lname, email,
                            "personalID", address, phone, pw, "username", "city", "province", country,
                            passportNumber, panelContactInfo.getDOB(), issuingCountry);
                    if (res) {
                        JOptionPane.showMessageDialog(null, "Register success");
                        closeMe();
                    } else {
                        JOptionPane.showMessageDialog(null, "Fail to register");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Bad input information");
                }
            }
        }, null);
        Tools.addJButton(panelFooter, "Cancel", null, 106, 100, 89, 23, null, null);
        Tools.centerFrame(this);
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                toFront();
            }
        });
    }

    public void closeMe() {
        this.dispose();
    }
}
